package com.china08.yxyapi.api.home.kecheng.sync;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLesson;
import com.china08.yxyapi.entity.mg.lessons.YxyLessonsUserScore;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroLessonRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonRepository;
import com.china08.yxyapi.repository.mg.YxyLessonsUserScoreRepository;

/**
 * 同步课程--观看视频页面--课程评价
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class KechengSyncLessonUserScoreController2 extends YxyApiBaseController {

    @Autowired
    private YxyLessonsUserScoreRepository yxyLessonsUserScoreRepository;

    /** 同步 */
    @Autowired
    private YxyLessonsSynchroLessonRepository yxyLessonsSynchroLessonRepository;

    /** 专题 */
    @Autowired
    private YxyLessonsTopicLessonRepository yxyLessonsTopicLessonRepository;

    /** 校本 */
    @Autowired
    private YxyLessonsSchoolLessonRepository yxyLessonsSchoolLessonRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/sync/user/score/crt2", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void find(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String userId = getProfile(request, response).getId();
        String lessonsId = reqModel.lessonsId;
        Integer score = reqModel.score;
        String id = userId + lessonsId;

        // 先判断有没有评价过 没评价就存入 算课程总评分
        YxyLessonsUserScore yxyLessonsUserScores = yxyLessonsUserScoreRepository.findByUserIdAndLessonsId(userId,
                lessonsId);
        Assert.isTrue(yxyLessonsUserScores == null, "您对此课程已经评价过了！");
        if (yxyLessonsUserScores == null) {
            yxyLessonsUserScores = new YxyLessonsUserScore();
            yxyLessonsUserScores.setId(id);
            yxyLessonsUserScores.setUserId(userId);
            yxyLessonsUserScores.setLessonsId(lessonsId);
            yxyLessonsUserScores.setScore(score);
            yxyLessonsUserScoreRepository.save(yxyLessonsUserScores);
        }

        // 课程评分（所有评分之和处于评分人数即综合评分）
        // 同步课程
        YxyLessonsSynchroLesson synchroLesson = yxyLessonsSynchroLessonRepository.findById(lessonsId);
        if (null != synchroLesson) {
            List<YxyLessonsUserScore> userScore = yxyLessonsUserScoreRepository.findByLessonsId(lessonsId);
            Integer score1 = 0;
            for (YxyLessonsUserScore yxyLessonsUserScore : userScore) {
                score1 += yxyLessonsUserScore.getScore();
            }
            // 计算总分存入课程表里
            Float ep = (float) score1 / (float) userScore.size();
            synchroLesson.setEp(ep);
            synchroLesson.setEpTimes(synchroLesson.getEpTimes() + 1);
            yxyLessonsSynchroLessonRepository.save(synchroLesson);
        }

        // 专题课程
        YxyLessonsTopicLesson topicLesson = yxyLessonsTopicLessonRepository.findById(lessonsId);
        if (null != topicLesson) {
            List<YxyLessonsUserScore> userScore = yxyLessonsUserScoreRepository.findByLessonsId(lessonsId);
            Integer score1 = 0;
            for (YxyLessonsUserScore yxyLessonsUserScore : userScore) {
                score1 += yxyLessonsUserScore.getScore();
            }
            // 计算总分存入课程表里
            Float ep = (float) score1 / (float) userScore.size();
            topicLesson.setEp(ep);
            topicLesson.setEpTimes(topicLesson.getEpTimes() + 1);
            yxyLessonsTopicLessonRepository.save(topicLesson);
        }

        // 校本课程
        YxyLessonsSchoolLesson schoolLesson = yxyLessonsSchoolLessonRepository.findById(lessonsId);
        if (null != schoolLesson) {
            List<YxyLessonsUserScore> userScore = yxyLessonsUserScoreRepository.findByLessonsId(lessonsId);
            Integer score1 = 0;
            for (YxyLessonsUserScore yxyLessonsUserScore : userScore) {
                score1 += yxyLessonsUserScore.getScore();
            }
            // 计算总分存入课程表里
            Float ep = (float) score1 / (float) userScore.size();
            schoolLesson.setEp(ep);
            schoolLesson.setEpTimes(schoolLesson.getEpTimes() + 1);
            yxyLessonsSchoolLessonRepository.save(schoolLesson);
        }

    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiPageReqModel {

        // 课程ID
        public String lessonsId;
        // 分数
        public Integer score;
    }

}
